package com.cisetech.web.bobocai.houtai;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cisetech.service.LoginService;
import com.cisetech.vo.bobocai.LUser;

//登录控制器
@Controller
@RequestMapping(value="/ht/login")
public class LoginController {
	@Autowired
	LoginService loginServiceImpl;
	//登录功能
	@RequestMapping(value="",method=RequestMethod.GET)
	public String index(HttpServletResponse response,HttpServletRequest request){
		return "houtai/login";
	}
	//登录校验
//	@ResponseBody
	@RequestMapping(value="/main",method=RequestMethod.POST)
	public String mainIndex(LUser user,HttpServletResponse response,HttpServletRequest request,Model model) throws ServletException, IOException{
		//ModelAndView v = new ModelAndView("houtai/index");
		System.out.println("userName=="+user.getName());
		String stats = loginServiceImpl.login(user,request);
		if("0".equals(stats)){
			return "redirect:/ht/index";
		}
		model.addAttribute("stats", stats);
		return "redirect:/ht/login";
	}
	//清除所有session并跳转到登录页面
	//登录功能
	@RequestMapping(value="/outUser",method=RequestMethod.GET)
	public String outUser(HttpServletResponse response,HttpServletRequest request,Model model){
		Enumeration<?> em = request.getSession().getAttributeNames();
		  while(em.hasMoreElements()){
		   request.getSession().removeAttribute(em.nextElement().toString());
		  }
		  model.addAttribute("stats", "");
		return "houtai/login";
	}
	//设置session中的按钮展开情况
	@RequestMapping(value="/setSessions",method=RequestMethod.POST)
	public void setSessions(HttpServletResponse response,HttpServletRequest request){
		String htmls = request.getParameter("htmls");
		request.getSession().setAttribute("resourceshtml", htmls);
	}
	public static LUser getUser(){
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		LUser user = (LUser)request.getSession().getAttribute("luser");
		return user;
	}
	public static HttpSession getSession(){
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getSession();
	}
}
